<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <jducastel@jducastel.fr>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* access control, based on sql beans
* user, group, userauthsession
* @author J.Ducastel <jducastel@jducastel.fr>
* @version 
*/
class accessControl_sqlBeans extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	*/
	function accessControl_sqlBeans() {
		$this->__construct();
	}/**/
	
	/**
	* "real" constructor
	* @access
	*/
	function __construct() {
		parent::__construct();
		$this->lib=&wak::getservice('waklib');
		$this->debug=&wak::getservice('wakdebug');
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* searches for user by login
	* @param string $login
	* @access public
	* @return bool
	*/
	function searchUserByLogin($login) {
		// searching for user
		$user=$this->makeUserBean();
		$user->set('login',$login);
		if ($user->load()) {
			$this->user=&$user;
			//echo wakdebug::vardump($user);
			return true;
		} else return false;
		//echo wakdebug::vardump($user);
	}/**/
	
	/**
	* checks pwd for current user
	* @param string $pwd
	* @access public
	* @return bool
	*/
	function checkPwd($pwd) {
		if (!$this->user) {
			return false;
		}
		return $this->user->checkPwd($pwd);
	}/**/
	
	/**
	* opens an auth session for current user
	* @param
	* @access public
	* @return bool
	*/
	function openSession($duration=null) {
		if (!is_int($duration))
			$duration=$this->cfg['defaults']['duration'];
		// getting current user
		if (!$this->user)
			return false;
		// creating session
		$this->session=$this->makeSessionBean();
		$this->session->set('user_id',$this->user->get('id'));
		$this->session->setDuration($duration);
		//echo wakdebug::vardump($session);
		//return true;
		if (!$this->session->save()) {
			return false;
		}
		// setting cookie ?
		return $this->setCookie();
		return true;
	}/**/
	
	/**
	* return user auth session. Retrieves it if necessary
	* 
	* @param
	* @access public
	* @return userAuthSessionBean|false
	*/
	function getAuthSession() {
		if ($this->session)
			return $this->session;
		// getting cookie
		if (!$id=$this->getCookie())
			return false;
		$session=$this->makeSessionBean();
		$session->set('id',$id);
		if ($session->load()) {
			$this->session=&$session;
		}
		return $session;
	}/**/
	
	/**
	* get current user. If not logged in, searches for session
	* @param
	* @access public
	* @return userBean|false
	*/
	function getUser() {
		if ($this->user)
			return $this->user;
		if (!$session=$this->getAuthSession())
			return false;
		if (!$user=$this->makeUserBean())
			return false;
		$user->set('id',$session->get('user_id'));
		
		if (!$user->load())
			//echo wakdebug::vardump($user);
			return false;
		// echo wakdebug::vardump($user);
		$this->user=&$user;
		return $user;
	}/**/
	
	/**
	* sets current user
	* @param databean $user
	* @access public
	* @return bool
	*/
	function setUser($user) {
		if (!is_a($user,'databean'))
			return false;
		$this->user=&$user;
		return true;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return bool
	*/
	function isLogged() {
		$session=$this->getAuthSession();
		//echo wakdebug::vardump($session);
		return $this->checkSession();
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return bool
	*/
	function closeSession() {
		$session=$this->getAuthSession();
		if (!$this->checkSession())
			return false;
		if (!$this->session->close())
			return false;
		return $this->removeCookie();
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var array config
	*/
	var $cfg=array(
		'cookie_name'=>'auth'
		, 'classes'=>array(
			'user'=>'userbean'
			, 'session'=>'userauthsessionbean')
		, 'defaults'=>array(
			'duration'=>86400)
	);
	
	/**
	* @var type desc
	*/
	var $session;
	/**
	* @var type desc
	*/
	var $user;
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* 
	* @param
	* @access private
	* @return userBean
	*/
	function makeUserBean() {
		$userclass=$this->cfg['classes']['user'];
		if (!$this->lib->needClass($userclass))
			return false;
		$user=&new $userclass();
		return $user;
	}/**/
	
	/**
	* sets session key cookie
	* @param
	* @access private
	* @return bool
	*/
	function setCookie() {
		if (!$this->session)
			return false;
		//echo "toto";
		$key=$this->cfg['cookie_name'];
		$value=$this->session->get('id');
		$limited=$this->session->get('limited');
		$RB=&wak::getservice('wakresponsebuilder');
		return $RB->setcookie($key,$value,$limited->toTimestamp());
	}/**/
	
	/**
	* retrieves auth session cookie
	* @param
	* @access private
	* @return string
	*/
	function getCookie() {
		$key=$this->cfg['cookie_name'];
		return isset($_COOKIE[$key])?$_COOKIE[$key]:false;
	}/**/
	
	/**
	* removes session cookie
	* @access protected
	* @return bool
	*/
	function removeCookie() {
		$key=$this->cfg['cookie_name'];
		$RB=&wak::getservice('wakresponsebuilder');
		return $RB->setcookie($key);
	}
	
	/**
	* 
	* @param
	* @access private
	* @return userauthsessionbean
	*/
	function makeSessionBean() {
		$sessionclass=$this->cfg['classes']['session'];
		if (!$this->lib->needClass($sessionclass))
			return false;
		$session=&new $sessionclass();
		return $session;
	}/**/
	
	/**
	* checks current session
	* @return bool
	*/
	function checkSession() {
		if (!$this->session)
			return false;
		if ($this->session->isFinished())
			return false;
		return true;
	}
}
?>
